import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';

class AnimatedSplashPage extends StatefulWidget {
  const AnimatedSplashPage({Key key}) : super(key: key);

  @override
  _AnimatedSplashPageState createState() => _AnimatedSplashPageState();
}

class _AnimatedSplashPageState extends State<AnimatedSplashPage> {
  bool showText = false;
  AssetImage image;
  @override
  void initState() {
    super.initState();
    image = AssetImage("assets/images/splash/splash_screen.gif");
    SchedulerBinding.instance.addPostFrameCallback((timeStamp) {
      playAnimation();
    });
  }

  @override
  void dispose() {
    image.evict();
    super.dispose();
  }

  playAnimation() async {
    await Future.delayed(Duration(milliseconds: 3800));
    if (mounted)
      setState(() {
        showText = true;
      });
    await Future.delayed(Duration(milliseconds: 1000));
    Routes.navigateTo("/home", clearStack: true, isSingleTop: true);
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        return false;
      },
      child: Scaffold(
        body: Stack(
          children: [
            Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Container(
                    width: 200,
                    height: 200,
                    padding: EdgeInsets.only(right: 10),
                    decoration:
                        BoxDecoration(image: DecorationImage(image: image)),
                  ),
                  AnimatedOpacity(
                    duration: Duration(milliseconds: 1000),
                    opacity: showText ? 1 : 0,
                    child: Text(
                      "Ace Accounts ®",
                      style:
                          TextStyle(fontSize: 28, fontWeight: FontWeight.w700),
                    ),
                  ),
                ],
              ),
            ),
            SafeArea(
              child: Align(
                alignment: Alignment.bottomCenter,
                child: Padding(
                  padding: const EdgeInsets.only(bottom: 10.0),
                  child: Text(
                    "Version ${Config.appVersion}",
                    style: TextStyle(
                      fontSize: 16,
                    ),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
